package com.china08.yxyapi.schedule.synresbase;

import java.net.UnknownHostException;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Profile;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import com.china08.yxyapi.entity.db.res.YxyResBookContents;
import com.china08.yxyapi.entity.db.yxy.YxyQuesKnow;
import com.china08.yxyapi.repository.db.YxyQuesKnowRepository;
import com.china08.yxyapi.repository.db.YxyResBookContentsRepository;
import com.china08.yxyapi.service.updateresource.QuesUpdateService;
import com.china08.yxyapi.service.updateresource.ResourceUpdateService;
import com.china08.yxyapi.service.updateresource.WeikeUpdateService;
import com.china08.yxyapi.util.YxyScheduleRunnableUtil;

@Profile(value = { "prohrb" })
@Component
@EnableScheduling
public class SynHrbBaseDataTask {

    @Autowired
    private ResourceUpdateService rus;

    @Autowired
    private WeikeUpdateService wus;

    @Autowired
    private QuesUpdateService qus;

    @Autowired
    private YxyQuesKnowRepository yxyQuesKnowRepository;

    @Autowired
    private YxyResBookContentsRepository yxyResBookContentsRepository;

    @Value("${spring.profiles.active}")
    String profiles;

    // @Scheduled(fixedRate = 1000 * 60 * 1)
    @Scheduled(cron = "0 5 3 * * ?")
    public void task() throws UnknownHostException {
        // ##
        // 检测定时任务是否可执行
        if (!YxyScheduleRunnableUtil.checkRunnable()) { return; }

        restBookcontent();
        restKnow();
    }

    public void restKnow() {
        //
        List<YxyQuesKnow> knowledges = yxyQuesKnowRepository.findByIsUpdate(1);
        if (knowledges.size() > 0) {
            wus.updateWeikeByKnowledges(knowledges);
            qus.updateQues(knowledges);
        }
    }

    public void restBookcontent() {
        List<YxyResBookContents> bookContents = yxyResBookContentsRepository.findByIsUpdate(1);
        if (bookContents.size() > 0) {
            rus.updateResourceByBookcontentId(bookContents);
        }
    }
}
